package androidx.gdata.blogger.xml;

import androidx.gdata.XMLSerializer;
import androidx.gdata.xml.LinkUrlSerializer;
import androidx.gdata.xml.GeneratorSerializer;
import androidx.gdata.xml.OpenSearchSerializer;
import androidx.gdata.xml.CategorySerializer;
import androidx.gdata.xml.AuthorSerializer;
import androidx.gdata.xml.TitleSerializer;
import androidx.gdata.xml.SubtitleSerializer;
import androidx.gdata.LinkUrl;
import androidx.gdata.Category;
import androidx.gdata.blogger.BlogEntryCollection;
import androidx.gdata.blogger.BlogEntry;

import java.io.PrintWriter;
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @author jennings
 *         Date: Nov 12, 2008
 */
public class BlogEntryCollectionSerializer implements XMLSerializer<BlogEntryCollection> {
  public static final String BLOGGER_FEED_PROLOG = "<feed xmlns='http://www.w3.org/2005/Atom'\n" +
      "      xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'>";

  private static LinkUrlSerializer linkSerializer = new LinkUrlSerializer();
  private static AuthorSerializer authorSerializer = new AuthorSerializer();
  private static GeneratorSerializer generatorSerializer = new GeneratorSerializer();
  private static OpenSearchSerializer openSearchSerializer = new OpenSearchSerializer();
  private static BlogEntrySerializer entrySerializer = new BlogEntrySerializer();
  private static CategorySerializer categorySerializer = new CategorySerializer();
  private static TitleSerializer titleSerializer = new TitleSerializer();
  private static SubtitleSerializer subtitleSerializer = new SubtitleSerializer();

  public BlogEntryCollectionSerializer() {
  }

  public void serialize(PrintWriter out, BlogEntryCollection blogEntryCollection) {
    out.println(XML_PROLOG);
    out.println(STYLESHEET);
    out.println(BLOGGER_FEED_PROLOG);
    out.println("  <id>" + blogEntryCollection.getId() + "</id>");
    out.println("  <updated>" + blogEntryCollection.getUpdated() + "</updated>");
    Category category = blogEntryCollection.getCategory();
    if (category != null) {
      categorySerializer.serialize(out, category);
    }
    titleSerializer.serialize(out, blogEntryCollection.getTitle());
    subtitleSerializer.serialize(out, blogEntryCollection.getSubtitle());
    for (LinkUrl link : blogEntryCollection.getLinks()) {
      linkSerializer.serialize(out, link);
    }
    authorSerializer.serialize(out, blogEntryCollection.getAuthor());
    generatorSerializer.serialize(out, blogEntryCollection.getGenerator());
    openSearchSerializer.serialize(out, blogEntryCollection.getOpenSearch());
    if (blogEntryCollection.getBlogEntries() != null) {
      for (BlogEntry entry : blogEntryCollection.getBlogEntries()) {
        entrySerializer.serialize(out, entry);
      }
    }
    out.println(FEED_EPILOG);
  }
}